import {Logger} from "../frame/apiModel/config/Logger";
import {InputStr, RewardType} from "../type/Type";
import {GlobalEnv} from "./GlobalEnv";
import {ItemJson} from "../frame/apiModel/model/StoreJson";
import {ModelFactory} from "../frame/apiModel/adapterModel/model5/ModelFactory";
import {AchievementController} from "../controller/AchievementController";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {TaskController} from "../controller/TaskController";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {AbilityController} from "../controller/AbilityController";
import {DataRouterUtil} from "./router/DataRouterUtil";
import {AttributeEnum, UnitAttribute} from "../frame/dataSource/DataModel";
import {UnitController} from "../controller/UnitController";
import UnitUtil from "./UnitUtil";
import MathUtil from "../frame/codeAdapter/MathUtil";


export class InputUtil {
    static doActionByStr (player: PlayerModel, name: string, value: string) {
        if (player.isLocalPlayer()) {
            Logger.toastError(`该命令在联机时会造成异步！请谨慎使用！`)
            if (name === InputStr.good) {
                const itemId = value;
                const unit = GlobalEnv.getHero(player);
                const itemJson: ItemJson = { id: itemId, n: 0, cd: 0, lv: 0 }
                if (unit?.isValid()) {
                    const item = ModelFactory.getInstance().createItemInUnit(itemJson, unit, undefined, true);
                    if (item) {
                        Logger.toastProduct('获得物品成功', player)
                    }
                }
            } else if (name === InputStr.ach) {
                const achievementId = value;
                const ret = AchievementController.getInstance().addAchievement(player, achievementId, 1);
                if (ret) {
                    Logger.toastProduct('获得成就成功', player)
                }
            } else if (name === InputStr.lv) {
                const unit = GlobalEnv.getHero(player);
                const upLv = MathUtil.toNumber(value);
                const currentLv = unit.level;
                unit.level = currentLv + upLv;
                Logger.toastProduct('增加等级成功', player)
            } else if (name === InputStr.coin) {
                const upCoin = MathUtil.toNumber(value);
                const currentCoin = player.gold;
                player.gold = currentCoin + upCoin;
                Logger.toastProduct('增加金币成功', player)
            } else if (name === InputStr.wood) {
                const upWood = MathUtil.toNumber(value);
                const currentLumber = player.lumber;
                player.lumber = currentLumber + upWood;
                Logger.toastProduct('增加木材成功', player)
            } else if (name === InputStr.integral) {
                const upIntegral = MathUtil.toNumber(value);
                AchievementController.getInstance().addIntegral(player, upIntegral);
                Logger.toastProduct('获得积分成功', player)
            } else if (name === InputStr.reward) {
                const rewardId = value;
                const hero = GlobalEnv.getHero(player);
                const ret = TaskController.getInstance().reward(hero, hero, RewardType.bag, rewardId)
                if (ret) {
                    Logger.toastProduct('获得奖励成功', player)
                }
            } else if (name === InputStr.kill) {
                const hero = GlobalEnv.getHero(player);
                hero.kill();
            } else if (name === InputStr.diff) {
                if (value) {
                    const numValue = MathUtil.toNumber(value);
                    GlobalEnv.difficult = numValue;
                    Logger.toastProduct('修改难度成功', player)
                }
            } else if (name === InputStr.wave) {
                if (value) {
                    const numValue = MathUtil.toNumber(value);
                    if (numValue === 99) {
                        const difficultData = DataRouterUtil.getDifficultData(GlobalEnv.difficult);
                        const wave = difficultData.wave;
                        const currentWave = GlobalEnv.wave;
                        const subWave = wave - currentWave;
                        GlobalEnv.addWave(subWave);
                    } else {
                        GlobalEnv.addWave(numValue);
                        Logger.toastProduct('添加波数成功', player)
                    }
                }
            } else if (name === InputStr.ability) {
                const abilityId = value;
                const hero = GlobalEnv.getHero(player);
                const ret = AbilityController.getInstance().learnAbilityCheck(hero, abilityId)
                if (ret) {
                    Logger.toastProduct('学习技能成功', player)
                }
            } else if (name === InputStr.invincibility) {
                const attr = new UnitAttribute();
                attr[AttributeEnum.attack] = 99999;
                attr[AttributeEnum.defense] = 99999;
                const hero = GlobalEnv.getHero(player);
                UnitUtil.addSubAttr(hero, attr);
                UnitController.getInstance().addCaluAttributeTaskByUnit(hero, false, '无敌');
            }
        }
    }
}